import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
let store= new Vuex.Store({
  state: {
    carPanelData:[],
     receiveInfo :[{
      "name": "王大锤",
      "phone": "13811111111",
      "areaCode": "010",
      "landLine": "64627856",
      "provinceId": 110000,
      "province": "北京市",
      "cityId": 110100,
      "city": "市辖区",
      "countyId": 110106,
      "county": "海淀区",
      "add": "上地十街辉煌国际西6号楼319室",
      "default": true,
      'checked': true
    },{
      "name": "李天一",
      "phone": "13757758258",
      "areaCode": "010",
      "landLine": "64627856",
      "provinceId": 110000,
      "province": "广东省",
      "cityId": 110100,
      "city": "纽约市",
      "countyId": 110106,
      "county": "朝阳区",
      "add": "华南理工大学",
      "default": false,
      'checked': false
    }],
    // provisionalOrder: [],
    orderData: [],
    maxOff:false,
    carShow:false,
    clearTimer:null,
    
  
    
  },
  getters:{
 totleCount(state){
  let count=0
  state.carPanelData.forEach((goods)=>{
    count+=goods.count
  })
  return count
 },
 totlePrice(state){
  let price=0
  state.carPanelData.forEach((goods)=>{
    price+=goods.count*goods.price
  })
  return price
 },
 allChecked(state){
  let allChecked=true
   state.carPanelData.forEach((goods)=>{
   if(!goods.checked){
     allChecked=false
     return
   }
  })
  return allChecked
 },
 checkedCount(state){
  let count=0
  state.carPanelData.forEach((goods)=>{
    if(goods.checked){
      count++
    }
  })
  return  count
 },
 checkedPrice(state){
  let total=0
  state.carPanelData.forEach((goods)=>{
    if(goods.checked){
      total+=goods.count*goods.price
    }
  })
  return total
 },
  checkedGoods(state){
  let checkedCarPanelData=[]
  state.carPanelData.forEach((goods)=>{
    if(goods.checked){
      checkedCarPanelData.push(goods)
    }
  })
  return checkedCarPanelData
  },
  // maxCount (state) {
  //     let maxOff = false
  //     state.carPanelData.forEach((goods) => {
  //       if(goods.count>goods.limit_num){
  //         goods.count--
  //       }
  //     })
  //     return maxOff
  //   }
  },
  mutations: {
   addCarPanelData(state,data){
      let flag =true
      state.carPanelData.forEach((goods)=>{
        if(goods.sku_id===data.info.sku_id){
          goods.count+=data.count
          flag = false
          if(goods.count>goods.limit_num)
        {
          goods.count-=data.count
          state.maxOff=true
          return
        }
        state.carShow=true
     
      }
      })
      if(flag){
        let goodsData=data.info
        Vue.set(goodsData,'count',data.count)
        Vue.set(goodsData,'checked',true)
        state.carPanelData.push(goodsData)
        state.carShow = true
        // state.ball.el = event.path[0]
        // state.ball.show = true
        // state.ball.img = data[0].inali_image
      }
     
    
      

    },
    delCarPanelData(state,id){
      state.carPanelData.forEach((goods)=>{
        if(goods.sku_id===id){
          state.carPanelData.splice('index',1)
          return
        }
      })
    },
    plusCarPanelData(state,id){
      state.carPanelData.forEach((goods)=>{
        if(goods.sku_id===id){
          if(goods.count>=goods.limit_num)return
          goods.count++
          return
        }
      })
    },
    subCarPanelData(state,id){
      state.carPanelData.forEach((goods)=>{
        if(goods.sku_id===id){
          if(goods.count<=1)return
          goods.count--
          return
        }
      })
    },
    checkGoods(state,id){
       state.carPanelData.forEach((goods)=>{
        if(goods.sku_id===id){
          goods.checked=!goods.checked
          return
        }
      })

    },
    allCheckGoods(state,allChecked){
      state.carPanelData.forEach((goods)=>{
        goods.checked=!allChecked
      })

    },
    delCheckGoods(state){
     
      let i=state.carPanelData.length
      while(i--){
        if(state.carPanelData[i].checked){
     state.carPanelData.splice(i,1)
        }
      }
       
    },
   
    submitOrder (state,data) {
      let i = state.carPanelData.length
      while(i--){
        if(state.carPanelData[i].checked){
          state.carPanelData.splice(i,1)
        }
      }
      state.orderData.unshift(data)
      state.receiveInfo.forEach((receive) => {
        receive.checked = receive.default
      })
    },
    payNow (state,id) {
      state.orderData.forEach((order) => {
        if(order.orderId === id){
          order.isPay = true
          return
        }
      })
    },
    alertPrompt (state) {
      state.maxOff = true
    },
    closePrompt(state){
      state.maxOff=false
    },
    showCar(state){
      clearTimeout(state.clearTimer)
      state.carShow=true
    },
    hideCar(state){
     state.clearTimer= setTimeout(() => {
        
        state.carShow=false
      }, 500);
   
    },
    checkDefault (state,data) {
      state.receiveInfo.forEach((receive) => {
        if (receive==data) {
          receive.default = true
       //state.receiveInfo.unshift(state.receiveInfo.splice(index,1)[0])
        } else {
          receive.default = false
        }
      })
    },
    // checkOut (state,data) {
    //   state.orderData= data
    // },
    submitReceive (state,data) {
      if(data[0].default){
        state.receiveInfo.forEach((receive) => {
          receive.default = false
          receive.checked = false
        })
      // state.receiveInfo.unshift(data)
      // return
      }
      if(data[1]==null){
        state.receiveInfo.push(data[0])
      }else{
        console.log(data[0])
        state.receiveInfo[data[1]] = data[0]
      }
    }
    
  }
})

export default store